#!/bin/bash
# date: 2016-08-09
# function:  For wsMS&Eagle check.
# Power by Levin Chen

# general info
NOW=`/bin/date '+[%Y/%m/%d %H:%M:%S]'`
#HOST='wsms_al_58.215.139.70'

# program info
MSG_DIR='/tmp/eagle_db1_test.txt'
MSGBK_DIR='/tmp/eagle_db2_test.txt'
ODP_DIR='/tmp/eagle_odp.txt'
LOG_PROG='/var/log/eagle_db_check.log'
ZB_HOST='zabbix.eagle.haplat.net'
ZBBK_HOST='zabbixbk.eagle.haplat.net'

#------ parameter for send to ODP ----------------------------
org='ms'
user='msadmin'
passwd=`echo -n ${user}@wsodp |md5sum|awk '{print$1}'`
odp_server="http://sendodp.haplat.net/write?db=${org}"
CURL='curl --connect-timeout 10 -m 10 -s -XPOST '

# mysql server
. /usr/local/eagle-scripts/eagle.ini
OPDBOPTS="-h $OPHOST --user=$OPUSER --password=$OPPASS --port=$OPPORT --default-character-set=gb2312 -N"

function sendmsg() {
    zabbix_server=$2
    zabbix_port=$3
    zabbix_sender='/usr/local/eagle-scripts/zabbix_sender.py'
    msg=$1
    $zabbix_sender -z $zabbix_server  -i $msg
}

function dataquery() {
  DBOPTS="$1"
  #TABLE="$2"
  result=`mysql $DBOPTS << EOF
  use wsms;
  SELECT
  	*
  FROM
  	(
  		SELECT
                        t.ALARM_NAME,CONCAT(':',COUNT(1))
  		FROM
  			CUR_ALARM t
  		GROUP BY
  			t.ALARM_NAME
  	) tmp
  WHERE
  	tmp.ALARM_NAME IN (
  		'Machine',
                'fwdlink',
  		'lighttpd',
  		'pollCrash',
  		'squid',
  		'fms',
  		'pollCrash_singlelink',
  		'VOD_ALARM_GLB',
  		'CS_machine',
  		'CS_poll',
  		'MAA_Perror',
  		'appa',
  		'new_qos_down',
  		'qos_down',
  		'unnomalvipCrash',
  		'pic_qos_down',
  		'portScan',
  		'maa_pollCrash',
  		'appa_port_conflict',
		'fms_healthy',
		'qos_down_web_15s',
		'qos_down_vod_15s',
		'pollCrash_15s',
		'livePollCrash_15s',
		'livePollCrash',
		'pic_qos_down_15s',
                'A_Failure_test',
                'qos_down_vod_40',
                'qos_down_vod_20'
  	);
  exit
EOF`
  echo $result
}

function schedulerStat() {
  DBOPTS="$1"
  #TABLE="$2"
  result=`mysql $DBOPTS << EOF
  use wsms;
  SELECT 'scheduler_poll_8b' AS TASK_TYPE,CONCAT(':', COUNT(1)) FROM TASK_ASSIGN_DETAIL_POLL_8B
  UNION
  SELECT 'scheduler_poll_100k' AS TASK_TYPE,CONCAT(':', COUNT(1)) FROM TASK_ASSIGN_DETAIL_POLL_100K
  UNION
  SELECT 'scheduler_poll_stream_live' AS TASK_TYPE,CONCAT(':', COUNT(1)) FROM TASK_ASSIGN_DETAIL_POLL_STREAM_LIVE
  UNION
  SELECT 'scheduler_scan_ssl_port' AS TASK_TYPE,CONCAT(':', COUNT(1)) FROM TASK_ASSIGN_DETAIL_SCAN_SSL_PORT
  UNION
  SELECT 'scheduler_poll_public_edge_view' AS TASK_TYPE,CONCAT(':', COUNT(1)) FROM TASK_ASSIGN_DETAIL_POLL_PUBLIC_EDGE_VIEW
        ;
  exit
EOF`
  echo $result
}

function curAlarmCheck() {
  DBOPTS="$1"
  #TABLE="$2"
  result=`mysql $DBOPTS wsms -e 'SELECT COUNT(1) FROM CUR_ALARM t'`
  echo "eagledb_$2 CURALARM $result" >> $MSG_DIR
  echo "CURALARM,host=${2},pro=eagle-wuxi value=$result" >> $ODP_DIR
}

function tbCountCheck() {
  DBOPTS="$1"
  TABLE="$2"
  result=`mysql $DBOPTS << EOF
  use wsms;
  SELECT COUNT(1) FROM $TABLE;
  exit
EOF`
  echo "eagledb_$3 $2 $result" >> $MSG_DIR
  echo "$2,host=${3},pro=eagle-wuxi value=$result" >> $ODP_DIR
}

function AllTbCount() {
  TB_CHECK='BASE_CONF_HOST BASE_SERVER_ALIVEFLOWIP BASE_SERVER_ALLLOGICIPINFO'

  for TB in $TB_CHECK
  do
    tbCountCheck "$OPDBOPTS" $TB $OPHOST
  done
}

function datacheck() {
  data="$1"
  param="$2"
  alram_name="$3"
  if [ "$data" = ''  ];  then
    mailmsg="$NOW $alram_name TABLE QUERY ERROR"
    #echo $mailmsg
    sendsms $SENDER "$mailmsg"
  else
    if [ "$data" -le "$param"  ];  then
      mailmsg="$NOW $alram_name TABLE NUM($data) is critical, less than $param"
      #echo $mailmsg
      sendsms $SENDER "$mailmsg"
    fi
  fi
}

function alarmNumCheck()  {
  TABLE=`dataquery "$1"`
  ALARM_NUM=`echo $TABLE | sed -e 's/\,$//g' -e 's/\ :/:/g'`
  for TBNUM in $ALARM_NUM
  do
    TBNUM_NEW=`echo "$TBNUM" | sed -e 's/:/\ /g'`
    TB_ODP=`echo "$TBNUM" | awk -F ':' '{print $1}'`
    TBNUM_ODP=`echo "$TBNUM" | awk -F ':' '{print $2}'`

    echo "eagledb_$2 $TBNUM_NEW" >> $MSG_DIR
    echo "$TB_ODP,host=${2},pro=eagle-wuxi value=$TBNUM_ODP" >> $ODP_DIR
  done    
}

function schedulerCheck()  {
  TABLE=`schedulerStat "$1"`
  ITEM_NUM=`echo $TABLE | sed -e 's/\,$//g' -e 's/\ :/:/g'`
  for TBNUM in $ITEM_NUM
  do
    TBNUM_NEW=`echo "$TBNUM" | sed -e 's/:/\ /g'`
    TB_ODP=`echo "$TBNUM" | awk -F ':' '{print $1}'`
    TBNUM_ODP=`echo "$TBNUM" | awk -F ':' '{print $2}'`

    echo "eagledb_$2 $TBNUM_NEW" >> $MSG_DIR
    echo "$TB_ODP,host=${2},pro=eagle-wuxi value=$TBNUM_ODP" >> $ODP_DIR
  done
}

function confScadaCheck()  {
  DBOPTS="$1"
  #TABLE="$2"
  updateTime=`mysql $DBOPTS << EOF
  use wsms;
  SELECT TIME_TO_SEC(
       (
        SELECT timediff(
                (SELECT date_format(NOW(), '%Y%m%d%H%i%s')),(SELECT VERSION FROM TASK_CONF_SCADA ORDER BY VERSION DESC LIMIT 1)
                )
        )
  )
        ;
  exit
EOF`
  echo "eagledb_$2 confOffsetTime $[$updateTime / 60]" >> $MSG_DIR
  echo "confOffsetTime,host=${2},pro=eagle-wuxi value=$[$updateTime / 60]" >> $ODP_DIR
}

rm -rf $MSG_DIR
rm -rf $MSGBK_DIR
rm -rf $ODP_DIR
alarmNumCheck "$OPDBOPTS" $OPHOST
#schedulerCheck "$OPDBOPTS" $OPHOST
curAlarmCheck "$OPDBOPTS" $OPHOST
AllTbCount
confScadaCheck "$OPDBOPTS" $OPHOST
#/bin/cp -a $MSG_DIR $MSGBK_DIR
#/bin/cp -a $MSG_DIR $MSTS_DIR
#sendmsg $MSG_DIR $ZB_HOST
#sendmsg $MSGBK_DIR $ZBBK_HOST
$CURL "${odp_server}" -u ${user}:${passwd} --data-binary @${ODP_DIR}
